package com.monkey_pig.memoryalbum.activity;

import android.graphics.Bitmap;
import android.media.ExifInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;

import com.monkey_pig.memoryalbum.R;
import com.monkey_pig.memoryalbum.activity.adapter.GridImageAdapter;
import com.monkey_pig.memoryalbum.activity.entity.ChildEntity;
import com.monkey_pig.memoryalbum.utils.BaseActivity;
import com.monkey_pig.memoryalbum.utils.BitmapUtils;
import com.monkey_pig.memoryalbum.utils.L;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.monkey_pig.memoryalbum.utils.MyUtils.getFDateTime;

public class GalleryActivity extends BaseActivity {

    @Bind(R.id.image_gallery)
    Gallery gallery;
    @Bind(R.id.image_current)
    ImageView currentImage;
    @Bind(R.id.pic_time)
    TextView picTime;

    private int[] thumbIds;
    private int currentPos;
    private Bitmap currentBitmap;
    private List<Bitmap> bitmapCache;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery);
        ButterKnife.bind(this);
        gallery.setOnItemClickListener(galleryItemClickListener);
        init();
    }

    private AdapterView.OnItemClickListener galleryItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> p, View v, int position, long id) {
            // 点击事件
            L.d("调用了item点击事件！！！！");
            showCurrentImage(position);
        }
    };

    private void init() {
        thumbIds = this.getIntent().getIntArrayExtra("thumbIds");
        currentPos = this.getIntent().getIntExtra("currentPos", 0);
        //galleryIds = this.getThumbnailIds(currentPos); //当前的gallery里的图片信息
        bitmapCache = BitmapUtils.queryThumbnailListByIds(this, thumbIds);
        List<ChildEntity> entities = new ArrayList<ChildEntity>();
        for (int i = 0; i < bitmapCache.size(); i++) {
            ChildEntity tempItem = new ChildEntity(bitmapCache.get(i));
            entities.add(tempItem);
        }
        GridImageAdapter adapter = new GridImageAdapter(this, entities);
        gallery.setAdapter(adapter);
        gallery.setSelection(currentPos);

        showCurrentImage(currentPos);

    }

    private void showCurrentImage(int position) {
        L.d("进入了show方法！！！！");
        if (currentBitmap != null) {
            currentBitmap.recycle();
        }

        currentBitmap = BitmapUtils.queryImageByThumbnailId(GalleryActivity.this, thumbIds[position]);
        String path = BitmapUtils.queryImagePathByThumbnailId(GalleryActivity.this,thumbIds[position]);
        L.d("当前图片的路径：",path);
        //获取拍照时间
        String FDateTime = getFDateTime(path);

        if (currentBitmap != null) {
            currentImage.setImageBitmap(currentBitmap);
            picTime.setText(FDateTime);
        } else {
            //什么都不做
        }

        //releaseBitmaps();
    }



}
